﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace TODO_LIST
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_gravar_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString);
            conn.Open();

            SqlCommand query = new SqlCommand("insert into lista_tarefas values(@tarefa, @feita)");
            query.Connection = conn;

            SqlParameter param1 = new SqlParameter("@tarefa", txt_tarefa.Text);
            SqlParameter param2 = new SqlParameter("@feita", false);

            query.Parameters.Add(param1);
            query.Parameters.Add(param2);

            query.ExecuteNonQuery();

            gridLista.DataBind();

            conn.Close();
        }

        protected void gridLista_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btn_editar_Click(object sender, EventArgs e)
        {
            gridLista.SelectRow(Int16.Parse(txt_edit.Text) - 1);
            gridLista.EditIndex = gridLista.SelectedIndex;
            
            btn_confirmar.Enabled = true;
            btn_cancelar.Visible = true;

            btn_editar.Visible = false;
            txt_edit.Visible = false;

            Session.Add("textoAntigo", gridLista.SelectedRow.Cells[0].Text);
            Session.Add("statusAntigo", gridLista.SelectedRow.Cells[1].Text);

            txt_tarefa.Text = gridLista.SelectedRow.Cells[0].Text;
        }

        protected void btn_cancelar_Click(object sender, EventArgs e)
        {
            gridLista.EditIndex = -1;

            btn_cancelar.Visible = false;
            btn_confirmar.Visible = false;
            btn_editar.Visible = true;
            txt_edit.Visible = true;

        }

        protected void btn_confirmar_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString);
            conn.Open();

            SqlCommand query = new SqlCommand("update lista_tarefas set tarefa = @pNovatarefa, feita = @pNovoStatus" +
            " where tarefa = @pTarefa and feita = @pStatus", conn);

            SqlParameter param1 = new SqlParameter("@pNovaTarefa", gridLista.SelectedRow.Cells[0].ToString());
            SqlParameter param2 = new SqlParameter("@pNovoStatus", gridLista.SelectedRow.Cells[1].ToString());
            SqlParameter param3 = new SqlParameter("@pTarefa", Session["textoAntigo"].ToString());
            SqlParameter param4 = new SqlParameter("@pStatus", Session["statusAntigo"].ToString());

            query.Parameters.Add(param1);
            query.Parameters.Add(param2);
            query.Parameters.Add(param3);
            query.Parameters.Add(param4);

            query.ExecuteNonQuery();
            gridLista.DataBind();
            conn.Close();
            gridLista.EditIndex = -1;
        }



    }
}
